package com.banzhi.shop.activity;

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.GroupGoodsDetailAdapter;
import com.banzhi.shop.adapter.HomeFrgAda;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.GroupProfitBean;
import com.banzhi.shop.data.GroupProfitData;
import com.banzhi.shop.entity.My.My;
import com.banzhi.shop.fragment.MyGroupingFragments;
import com.banzhi.shop.fragment.MyGroupingFragments2;
import com.banzhi.shop.model.CallBack.GroupProfitCallBack;
import com.banzhi.shop.model.Impl.GroupProfitImpl;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.TabLayoutUtil;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.NoScrollViewPager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

//我的拼团
public class MyGroupingActivity2 extends BaseActivity implements View.OnClickListener, GroupProfitCallBack {
    private ImageView mBack;
    //我的拼团
    private TextView mTvMyGrouping;
    //拼团收益
    private TextView mTvGroupingProfit;
   // private ImageView mIvRefresh;
    private LinearLayout mLlMyGrouping;
    private TabLayout mTabsGrouping;
    private NoScrollViewPager mVpGrouping;
    private LinearLayout mLlGroupingProfit;
    private TabLayout mTabsGroupingProfit;
    private NoScrollViewPager mVpGroupingProfit;
    private GroupProfitImpl mModel;

    private TextView mTvTotal;

    private String mTotalprice;
    private String mTotalprofit;
    private GroupProfitData mGroupProfitData;
    private GroupProfitBean mGroupProfitBean;
    private TextView mTvTotalPrice;
    private TextView mTvTotalProfit;
    private LinearLayout mLlgoods;
    private ImageView mIvLineWhite;
    private LinearLayout mLlProfit;
    private ImageView mIvLineWhites2;
    private LinearLayout mLlProfitDetail;
    private LinearLayout mLlGoodsDetail;
    private SmartRefreshLayout mSmartRefreshLayout;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mSmartRefreshLayout2;
    private RecyclerView mRecyclerView2;
    private List<GroupProfitBean.DataBean> mDataBeanList;
    private List<GroupProfitBean.DataBean> mDataBeans = new ArrayList<>();
    private int page = 1;
    private GroupGoodsDetailAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    private RelativeLayout rl_nodata;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_grouping2);
        Translucent.setTranslucent(this);
        initView();
        initListener();
        mModel = new GroupProfitImpl();
        LoadingDialogUtils.showProgress(MyGroupingActivity2.this, "正在加载,请稍后~");
        mModel.getGroupProfitData(1 + "", page+"", 20 + "", this);
    }
    @Override
    protected void onStart() {
        super.onStart();
        mTabsGrouping.post(new Runnable() {
            @Override
            public void run() {
                TabLayoutUtil.setIndicator(mTabsGrouping, 7, 7);
            }
        });
    }

    private void initView() {
        mBack = (ImageView) findViewById(R.id.iv_back_grouping);
        mTvMyGrouping = (TextView) findViewById(R.id.tv_my_grouping);
        mTvGroupingProfit = (TextView) findViewById(R.id.tv_grouping_profit);
      //  mIvRefresh = (ImageView) findViewById(R.id.iv_refresh);
        mLlMyGrouping = (LinearLayout) findViewById(R.id.ll_grouping);
        mTabsGrouping = (TabLayout) findViewById(R.id.tabs_grouping);
        mVpGrouping = (NoScrollViewPager) findViewById(R.id.vp_grouping);
        mLlGroupingProfit = (LinearLayout) findViewById(R.id.ll_grouping_profit);
        //   mTabsGroupingProfit = (TabLayout) findViewById(R.id.tabs_grouping_profit);
        //  mVpGroupingProfit = (NoScrollViewPager) findViewById(R.id.vp_grouping_profit);
        //  mVpGrouping.setOffscreenPageLimit(3);

        mTvTotal = (TextView) findViewById(R.id.tv_total);
        mTvTotalPrice = (TextView) findViewById(R.id.tv_total_price);
        mTvTotalProfit = (TextView) findViewById(R.id.tv_total_profit);

        mLlgoods = (LinearLayout) findViewById(R.id.ll_goods);
        mIvLineWhite = (ImageView) findViewById(R.id.iv_line_white);

        mLlProfit = (LinearLayout) findViewById(R.id.ll_profit);
        mIvLineWhites2 = (ImageView) findViewById(R.id.iv_line_white2);

        mLlGoodsDetail = (LinearLayout) findViewById(R.id.ll_goods_detail);
        mLlProfitDetail = (LinearLayout) findViewById(R.id.ll_profit_detail);

        mSmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smart_refresh);
        mSmartRefreshLayout2 = (SmartRefreshLayout) findViewById(R.id.smart_refresh2);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView2 = (RecyclerView) findViewById(R.id.recycler_view2);
        rl_nodata = (RelativeLayout) findViewById(R.id.rl_nodata);


        //我的拼团
        ArrayList<Fragment> fragments = new ArrayList<>();
        //   0 全部 1拼团中 2拼团成功 3拼团失败4拼团售后
        MyGroupingFragments2 fragments0 = new MyGroupingFragments2().newInstance("0");
        MyGroupingFragments2 fragments1 = new MyGroupingFragments2().newInstance("1");
        MyGroupingFragments2 fragments2 = new MyGroupingFragments2().newInstance("2");
        MyGroupingFragments2 fragments3 = new MyGroupingFragments2().newInstance("3");
        MyGroupingFragments2 fragments4 = new MyGroupingFragments2().newInstance("4");
        fragments.add(fragments0);
        fragments.add(fragments1);
        fragments.add(fragments2);
        fragments.add(fragments3);
        fragments.add(fragments4);

        ArrayList lists = new ArrayList<>();
        lists.add("全部");
        lists.add("拼团中");
        lists.add("拼团成功");
        lists.add("拼团失败");
        lists.add("拼团售后");

        HomeFrgAda homeFrgAda = new HomeFrgAda(this.getSupportFragmentManager(), fragments, lists);
        mVpGrouping.setAdapter(homeFrgAda);
        mTabsGrouping.setupWithViewPager(mVpGrouping);
    }

    private void initListener() {
        mBack.setOnClickListener(this);
        mTvMyGrouping.setOnClickListener(this);
        mLlMyGrouping.setOnClickListener(this);
        mLlGroupingProfit.setOnClickListener(this);
        mTvGroupingProfit.setOnClickListener(this);
      //  mIvRefresh.setOnClickListener(this);
        mLlgoods.setOnClickListener(this);
        mLlProfit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_grouping:
                finish();
                break;
            case R.id.tv_my_grouping:
                mTvMyGrouping.setBackgroundResource(R.mipmap.bg1);
                mTvGroupingProfit.setBackgroundResource(R.mipmap.bg2);
                mTvGroupingProfit.setTextColor(Color.WHITE);
                mLlGroupingProfit.setVisibility(View.GONE);
                mLlMyGrouping.setVisibility(View.VISIBLE);
                mTvMyGrouping.setTextColor(getResources().getColor(R.color.themeColor));

                break;
            case R.id.tv_grouping_profit:
                mTvMyGrouping.setBackgroundResource(R.mipmap.bg4);
                mTvGroupingProfit.setBackgroundResource(R.mipmap.bg5);
                mTvGroupingProfit.setTextColor(getResources().getColor(R.color.themeColor));
                mLlMyGrouping.setVisibility(View.GONE);
                mLlGroupingProfit.setVisibility(View.VISIBLE);
                mTvMyGrouping.setTextColor(Color.WHITE);
                break;
            case R.id.ll_goods:
                mIvLineWhite.setVisibility(View.VISIBLE);
                mIvLineWhites2.setVisibility(View.INVISIBLE);
                mLlGoodsDetail.setVisibility(View.VISIBLE);
                mLlProfitDetail.setVisibility(View.GONE);

                break;
            case R.id.ll_profit:
                mIvLineWhite.setVisibility(View.INVISIBLE);
                mIvLineWhites2.setVisibility(View.VISIBLE);

                mLlGoodsDetail.setVisibility(View.GONE);
                mLlProfitDetail.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void ongetGroupProfitDataSuccess(Object object) {
        mGroupProfitBean = (GroupProfitBean) object;
        mDataBeanList = mGroupProfitBean.getData();

        if ( page == 1) {
            mDataBeans.clear();
        }else {
            if (mDataBeanList.size() == 0) {
                return;
            }
        }
        mDataBeans.addAll( mDataBeanList);
        if (mDataBeans.size() == 0) {
            rl_nodata.setVisibility(View.VISIBLE);
        } else {
            rl_nodata.setVisibility(View.GONE);
        }
        showView();
    }

    private void showView() {
        mTvTotal.setText(mGroupProfitBean.getTotal().getTotal());
        mTotalprice = mGroupProfitBean.getTotal().getTotalprice();
        mTotalprofit = mGroupProfitBean.getTotal().getTotalprofit();
        mTvTotalPrice.setText(mTotalprice);
        mTvTotalProfit.setText(mTotalprofit);
        mAdapter = new GroupGoodsDetailAdapter(mDataBeans);
        mLayoutManager = new LinearLayoutManager(MyGroupingActivity2.this);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
        setRefreshListener();

    }


    private void setRefreshListener() {
        //设置 Header 为 MaterialHeader
        mSmartRefreshLayout.setRefreshHeader(new MaterialHeader(this));
        //设置 Footer 为 经典样式
        mSmartRefreshLayout.setRefreshFooter(new ClassicsFooter(this));
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(1000);
                page=1;

                mModel.getGroupProfitData(1 + "", page+"", 20 + "", MyGroupingActivity2.this);
                mDataBeans.clear();
                for (int i = 0; i < mDataBeanList.size(); i++) {
                    mDataBeans.add(mDataBeanList.get(i));
                }
                //刷新试图
                mAdapter.notifyDataSetChanged();
            }
        });
        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                LogUtils.e("PAGE:", page + "");
                LoadingDialogUtils.showProgress(MyGroupingActivity2.this, "正在加载,请稍后~");
                mModel.getGroupProfitData(1 + "", page+"", 20 + "", MyGroupingActivity2.this);
                refreshlayout.finishLoadmore(1000);
                //试图获取总条目数
                int count = mAdapter.getItemCount();
                int size = mDataBeans.size();
                //如果当前试图总条目== 集合长度
                if (count == size) {
                    // Toast.makeText(getContext(), "数据以空", Toast.LENGTH_SHORT).show();
                }
                //当前试图总条目+20就是 一次加载20条目
                if (count + 20 < size) {
                    for (int i = count; i < (count + 20); i++) {
                        mDataBeans.add(mDataBeanList.get(i));
                    }
                } else {
                    for (int i = count; i < size; i++) {
                        mDataBeans.add(mDataBeanList.get(i));
                    }
                }
                mAdapter.notifyDataSetChanged();

            }
        });
    }

    @Override
    public void ongetGroupProfitDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(MyGroupingActivity2.this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }
}
